//
//  AuthorHelper.m
//  Kaagad Peso
//
//  Created by ZhengChengFeng on 2024/10/15.
//

#import "AuthorHelper.h"
#import <CoreLocation/CLLocationManager.h>
#import <AVFoundation/AVCaptureDevice.h>


#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>

#import <Contacts/CNContactStore.h>
#import <ContactsUI/CNContactPickerViewController.h>


@implementation AuthorHelper

+ (BOOL)determineWhetherTheAPPOpensTheLocation{
    
    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {
        return YES;
    }
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kStoreLongitude];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kStoreLatitude];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    AlertView *alertV = [[AlertView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withTitleStr:[NSString stringWithFormat:@"%@%@",kAppName,@" needs to request location permission in order to identify your area and provide loan products and services that are more suitable for you."] withCancelStr:@"Cancel" withSureStr:@"Turn On"];
    alertV.clickSureBlock = ^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
    };
    [[UIApplication sharedApplication].keyWindow addSubview:alertV];
   
    return NO;
}

+ (void)isCanUseCamera:(VoidBlock)block{
   
    AVAuthorizationStatus authStatus =  [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    if (authStatus == AVAuthorizationStatusDenied){
        
        AlertView *alertV = [[AlertView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withTitleStr:[NSString stringWithFormat:@"%@%@",kAppName,@" needs to request camera permission to take photos of documents and scan your face to verify your true identity."] withCancelStr:@"Cancel" withSureStr:@"Turn On"];
        alertV.clickSureBlock = ^{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
        };
        [[UIApplication sharedApplication].keyWindow addSubview:alertV];
        
        return;
        
    }
    
    else if (authStatus == AVAuthorizationStatusNotDetermined){
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {
                if (block) {
                    block();
                }
            } 
            
        }];
        return;
    }
 
    else {
        if (block) {
            block();
        }
    }

}

+ (BOOL)isOpenContact{
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
   
    if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
        
        
        AlertView *alertV = [[AlertView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withTitleStr:[NSString stringWithFormat:@"%@%@",kAppName,@" needs to request your contact permission to verify the authenticity of your identity, help you quickly complete the certification process, and also help you pass the loan application."] withCancelStr:@"Cancel" withSureStr:@"Turn On"];
        alertV.clickSureBlock = ^{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
        };
        [[UIApplication sharedApplication].keyWindow addSubview:alertV];
        
        return NO;
    }
    
    return YES;
}

@end
